/**
 * @program: LeetCode
 * @description: LeetCode : 24. 两两交换链表中的节点
 * @author: WXY
 * @create: 2023-01-14 14:27
 * @Version 1.0
 **/
public class Num024_swapPairs {
    public static ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode listHead = new ListNode(0);
        listHead.next = head;
        ListNode cur = listHead;
        while (cur.next != null && cur.next.next != null) {
            ListNode curNext = cur.next;
            ListNode curNextNext = curNext.next;
            cur.next = curNextNext;
            curNext.next = curNextNext.next;
            curNextNext.next = curNext;
            cur = cur.next.next;
        }
        return listHead.next;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        public ListNode(int val) {
            this.val = val;
        }
    }
}
